package jmine.tec.services.utils.replace.hssf;

import jmine.tec.services.utils.replace.api.ServiceHeader;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class HSSFServiceHeader implements ServiceHeader {

    private final HSSFRow row;

    private int size;

    public HSSFServiceHeader(HSSFRow row) {
        this.row = row;
        for (int i = 3;; i++) {
            if (this.row.getCell(i) == null) {
                this.size = i;
                break;
            }
        }
    }

    void addParameter(String parameter) {
        HSSFCell moved = this.row.getCell(this.size);
        Integer index = this.size;
        while (moved != null) {
            index++;
            HSSFCell next = this.row.getCell(index);
            this.row.moveCell(moved, index.shortValue());
            moved = next;
        }
        HSSFCell cell = this.row.createCell(this.size);
        cell.setCellValue(new HSSFRichTextString(parameter));
        this.size++;
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.IServiceHeader#renameParameter(java.lang.String, java.lang.String)
     */
    public void renameParameter(String currentName, String newName) {
        Integer index = this.getParameterIndex(currentName);
        if (index == null) {
            return;
        }
        HSSFCell cell = this.row.getCell(index);
        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            String currentValue = cell.getRichStringCellValue().getString();
            if (currentName.equals(currentValue)) {
                cell.setCellValue(new HSSFRichTextString(newName));
                return;
            }
        }
    }

    void removeParameter(String parameter) {
        Integer index = this.getParameterIndex(parameter);
        if (index == null) {
            return;
        }
        this.row.removeCell(this.row.getCell(index));
        while (index <= this.size) {
            HSSFCell moved = this.row.getCell(index + 1);
            if (moved != null) {
                this.row.moveCell(moved, index.shortValue());
            }
            index++;
        }
        this.size--;
    }

    public Integer getParameterIndex(String parameter) {
        for (int cellIndex = 2; this.row.getCell(cellIndex) != null; cellIndex++) {
            HSSFCell cell = this.row.getCell(cellIndex);
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING && parameter.equals(cell.getRichStringCellValue().getString())) {
                return cellIndex;
            }
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.IServiceHeader#size()
     */
    public int size() {
        return this.size;
    }

    public boolean hasParameter(String parameter) {
        return this.getParameterIndex(parameter) != null;
    }
}
